﻿Imports Lourdes.BackEnd.BC
Imports Lourdes.BackEnd.Interfaces
Imports Lourdes.BackEnd.View
Imports Lourdes.BackEnd.Modelo
Imports Lourdes.BackEnd
Public Class ucFormaPago

  Private _MontoTotal As Double
  Public Property MontoTotal() As Double
    Get
      Return _MontoTotal
    End Get
    Set(ByVal value As Double)
      _MontoTotal = value
      If UsaCuotas() Then
        ConfiguraCuotasGrilla(False)
      Else
        ConfiguraCuotasGrilla(True)
      End If

    End Set
  End Property

  Private Sub ucFormaPago_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not DesignMode Then
      ConfigurarControles()
    End If
  End Sub

  Private Sub ConfigurarControles()
    Try
      txtFechaFacturacion.Value = Now
      txtPrimerPago.Value = Now
      With (cmbFormasPago)
        .DataSource = (New BC_FormaPago).GetAll
        .ValueMember = "FormaPagoId"
        .DisplayMember = "Nombre"
      End With
      CuotasControlsManager(UsaCuotas)
    Catch ex As Exception
      Throw ex
    End Try
  End Sub

  Private Sub CuotasControlsManager(ByVal pEnable As Boolean)
    Try
      If Not pEnable Then
        With txtCantidadCuotas
          .Value = 1
          .ReadOnly = True
          .Enabled = False
        End With
        With txtPrimerPago
          .Nullable = True
          .NullText = ""
          .ReadOnly = True
          .Enabled = False
        End With
        btnGenerarCuotas.Enabled = False
        ConfiguraCuotasGrilla(True)
        grdCuotas.DisplayLayout.Override.AllowUpdate = Infragistics.Win.DefaultableBoolean.False
      Else
        With txtCantidadCuotas
          .Value = 1
          .ReadOnly = False
          .Enabled = True
        End With
        With txtPrimerPago
          .Value = Now.AddMonths(1)
          .Nullable = False
          .ReadOnly = False
          .Enabled = True
        End With
        btnGenerarCuotas.Enabled = True
        grdCuotas.DisplayLayout.Override.AllowUpdate = Infragistics.Win.DefaultableBoolean.True

      End If
    Catch ex As Exception
      Throw ex
    End Try
  End Sub


  Public ReadOnly Property FormaPago As Integer
    Get
      Return CInt(cmbFormasPago.SelectedValue)
    End Get
  End Property

  Public ReadOnly Property FechaFacturacion As DateTime
    Get
      Return txtFechaFacturacion.DateTime
    End Get
  End Property

  Private Sub cmbFormasPago_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbFormasPago.SelectionChangeCommitted
    CuotasControlsManager(UsaCuotas)
  End Sub

  Private Function UsaCuotas() As Boolean
    Return CInt(Utils.GetValueConf("FormaPagoIdConCuotas")) = CInt(cmbFormasPago.SelectedValue)
  End Function

  Private Sub ConfiguraCuotasGrilla(ByVal pPagoContado As Boolean)
    Dim lstCuotas As lstPagosCuotas
    Try
      lstCuotas = New lstPagosCuotas
      If Not pPagoContado Then
        lstCuotas.CargarConCuotas(txtCantidadCuotas.Value, _MontoTotal, txtPrimerPago.DateTime)
      Else
        lstCuotas.CargarConCuotas(1, _MontoTotal, txtPrimerPago.DateTime)
      End If
      grdCuotas.DataSource = lstCuotas
      grdCuotas.DataBind()
    Catch ex As Exception
      Utils.ManejoErrores(ex)
    End Try
  End Sub

  'Private Sub txtCantidadCuotas_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCantidadCuotas.LostFocus
  '  If grdCuotas.Rows.Count <> txtCantidadCuotas.Value Then
  '    If MsgBox("¿Desea que el sistema realice el calculo de cuotas?", MsgBoxStyle.OkCancel, "Calculo de Cuotas") = MsgBoxResult.Ok Then
  '      ConfiguraCuotasGrilla(False)
  '    End If
  '  End If
  'End Sub

  Private Sub btnGenerarCuotas_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerarCuotas.Click
    ConfiguraCuotasGrilla(False)
  End Sub


  Public Sub Save(ByVal pDocId As Integer)
    Dim objPagosBC As BC_Pagos
    Try
      objPagosBC = New BC_Pagos
      If BC_Pagos.PagoModificable(pDocId) Then
        objPagosBC.AddOneFromFormaPagoCuotas(pDocId, CType(grdCuotas.DataSource, lstPagosCuotas))
      End If
    Catch ex As Exception
      Throw ex
    End Try
  End Sub

  Public Sub LimpiarUCFormaDePago()
    ConfigurarControles()
  End Sub

  Public Sub CargarFormaDePago(pCabecera As vFacturaCabecera)
    Dim olstPagosCuotas As lstPagosCuotas
    Dim objPagosBC As BC_Pagos
    objPagosBC = New BC_Pagos


    With pCabecera
      olstPagosCuotas = objPagosBC.GetPagosFactura(.FacturaBase.DocId, .FacturaBase.TotalNeto)
      cmbFormasPago.SelectedValue = .FacturaBase.FormaPagoId
      Me.txtFechaFacturacion.Value = .FechaFacturacion
      Me.txtPrimerPago.Value = olstPagosCuotas.FirstOrDefault().Fecha
      Me.txtCantidadCuotas.Value = olstPagosCuotas.Count
      Me.grdCuotas.DataSource = olstPagosCuotas
    End With

  End Sub
End Class
